package com.museum.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.museum.base.BaseResponse;
import com.museum.controller.APi.feignAPI.FeignAPIController;
import com.museum.core.utils.VeDate;
import com.museum.entity.DO.Clue;
import com.museum.entity.DO.ColProcess;
import com.museum.entity.DTO.CommitCol;
import com.museum.entity.DTO.QueryCol;

import com.museum.mapper.ProcessMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Ruiwen
 * @since 2021-01-05
 */
@Service
public class ClueServiceImpl extends ServiceImpl<com.museum.mapper.ClueMapper, Clue> {
     @Autowired
     private FeignAPIController feignAPIController;
     @Autowired
     private ProcessMapper processService;
     @Autowired
     private com.museum.mapper.ClueMapper clueMapper;
    @Autowired
    private ProcessMapper processMapper;

    /**
     * @param
     * @param request
     * @Desciption 添加征集
     * @return
     */
    public int addClue(Clue clue, HttpServletRequest request) {
      String token = request.getHeader("Authorization");
      BaseResponse<JSONObject> userMessageByUSerId = feignAPIController.getUserMessageByUSerId(token);
      JSONObject data = userMessageByUSerId.getData();
      clue.setUserName((String)data.get("realName"));
      this.baseMapper.insert(clue);
      //一表多增
      int processId  = clue.getId();
      ColProcess process = new ColProcess();
      process.setId(processId);
      process.setOperateUser((String)data.get("realName"));
      process.setAreaNumber((Integer)data.get("areaNumber"));
      process.setOperateTime(VeDate.getStringDateShort());
      process.setStatus(0);
      process.setOperateStatus(0);
      process.setWay(clue.getWay());

      process.setClueName(clue.getClueName());
      process.setClueTime(clue.getCreateTime());
      process.setPriceStatus(0);
      process.setCollectionStatus(0);
      return processService.insert(process);
    }


    /**
     * @param
     * @Desciption 条件查询征集
     * @return
     */
    public JSONObject getAllColByPage(long current, long limit,QueryCol queryCol){
        HashMap<String,Object> map =  new HashMap<String,Object>();
        List<Clue> clueList = new ArrayList<>();
        Integer id =null;
        String clueName =null;
        String sourceCome = null;
        String relativeName =null;
        String relativePhone =null;
        Integer sortId = null;
        if(queryCol!=null){
           id = queryCol.getId();
           clueName = queryCol.getClueName();
           sourceCome = queryCol.getSourceCome();
           relativeName = queryCol.getRelativeName();
           relativePhone = queryCol.getRelativePhone();
            sortId = queryCol.getSortId();
        }
        if (id == null || id == 0) {
            id = null;
        }
        if (sortId == null || sortId == 0) {
            sortId = null;
        }
        if (!StringUtils.isEmpty(id)) {
            map.put("id",id);
        }
        if (!StringUtils.isEmpty(clueName)) {
            map.put("clueName",clueName);
        }
        if (!StringUtils.isEmpty(sourceCome)) {
            map.put("sourceCome",sourceCome);
        }
        if (!StringUtils.isEmpty(relativeName)) {
            map.put("relativeName",relativeName);
        }
        if (!StringUtils.isEmpty(relativePhone)) {
            map.put("relativePhone",relativePhone);
        }
        if (!StringUtils.isEmpty(sortId)) {
            map.put("sort",sortId);
        }
        map.put("operate_status",0);
        map.put("current", (current - 1) * limit);
        map.put("limit",limit);
        List<Clue> list = clueMapper.selectColByPage(map);
        for (Clue clue : list) {
            int clueId = clue.getId();
            Integer status = processMapper.getStatus(clueId);
            if(processMapper.isOperateStauts(clueId)&&status!=-1){
                clueList.add(clue);
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",clueList.size());
        jsonObject.put("ClueList",clueList);
        return jsonObject;
    }
    /**
     * @param
     * @Desciption 删除征集
     * @return
     */
    @Transactional
    public int deleteColById(Integer id){
        processMapper.deleteById(id);
        return clueMapper.deleteById(id);
    };
    /**
     * @param
     * @Desciption 提交征集 状态转换为 1
     * @return
     */
    public int commitCol(CommitCol commitcol, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        BaseResponse<JSONObject> userMessageByUSerId = feignAPIController.getUserMessageByUSerId(token);
        JSONObject data = userMessageByUSerId.getData();
        Integer id = commitcol.getId();
        ColProcess process = clueMapper.selectProcessById(id);
       String operate_desc =  commitcol.getDesc();
       if(StringUtils.isEmpty(operate_desc)){
           operate_desc ="此人无留言.";
       }
       Integer status = commitcol.getStatus();
       if(status==-1){
           process.setStatus(-1);
       }
       processMapper.updateOperateDesc(operate_desc,id);
       processMapper.commitCol(id,status);
       return processMapper.updateById(process);
    }

}
